package cn.iocoder.yudao.module.wms.service.otherin;

import cn.iocoder.yudao.framework.common.enums.ApproveStatusEnum;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.base.api.ProductInfoApi;
import cn.iocoder.yudao.module.base.api.dto.ProductInfoRespDTO;
import cn.iocoder.yudao.module.wms.dal.dataobject.otherin.OtherInDDO;
import cn.iocoder.yudao.module.wms.dal.dataobject.otherin.OtherInDO;
import cn.iocoder.yudao.module.wms.dal.mysql.otherin.OtherInMapper;
import cn.iocoder.yudao.module.wms.service.inventory.InventoryAbstract;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.OTHER_IN_NOT_CANCEL;

/**
 * @Author: Ljx
 * @createTime: 2024年05月05日 14:25:30
 * @version: v1.0.0
 * @Description: 其他入库单撤审状态
 */
@Component(value = "false")
public class OtherInCancelStatusImpl extends OtherInStatusAbstract {

    @Resource
    private OtherInMapper otherInMapper;
    @Resource
    private ProductInfoApi productInfoApi;
    @Resource
    private Map<String, InventoryAbstract> inventoryMap;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void approve(OtherInDO otherIn, List<OtherInDDO> otherInDs) {
        otherIn.setApproveStatus(ApproveStatusEnum.APPROVE.getStatus());
        otherIn.setApproveDate(LocalDate.now());
        otherIn.setApproveUser(SecurityFrameworkUtils.getLoginUserId());
        otherInMapper.updateById(otherIn);
        otherInDs.forEach(otherInD -> {
            ProductInfoRespDTO productInfo = productInfoApi.getProductInfo(otherInD.getProductId());
            inventoryMap.get(productInfo.getManageType().toString()).increaseInventory(otherInD.getProductId(), otherInD.getWarehouseId(), otherInD.getPositionId(), otherInD.getBusinessQty(), otherInD.getUnitAmt(), otherInD.getCostAmt(), otherInD.getBatchNo(), otherInD.getSerialNo(), otherIn.getId(), otherIn.getCode(), otherInD.getSequence());
        });

    }

    @Override
    public void cancel(OtherInDO otherIn, List<OtherInDDO> otherInDs) {
        throw exception(OTHER_IN_NOT_CANCEL);
    }
}
